package com.lyh.crm.settings.service.impl;

import com.lyh.crm.settings.dao.DicTypeDao;
import com.lyh.crm.settings.dao.DicValueDao;
import com.lyh.crm.settings.domain.DicType;
import com.lyh.crm.settings.domain.DicValue;
import com.lyh.crm.settings.service.DicService;
import com.lyh.crm.utils.DateTimeUtil;
import com.lyh.crm.utils.PrintJson;
import com.lyh.crm.utils.SqlSessionUtil;
import com.lyh.crm.utils.UUIDUtil;
import com.lyh.crm.workbench.domain.Contacts;
import com.lyh.crm.workbench.domain.Customer;

import java.util.*;

public class DicServiceImpl implements DicService {

    private DicTypeDao dicTypeDao = SqlSessionUtil.getSqlSession().getMapper(DicTypeDao.class);
    private DicValueDao dicValueDao = SqlSessionUtil.getSqlSession().getMapper(DicValueDao.class);

    public Map<String, List<DicValue>> getAll() {

        Map<String, List<DicValue>> map = new HashMap<String, List<DicValue>>();

        //将字典类型列表取出
        List<DicType> dtList = dicTypeDao.getTypeList();

        //将字典类型列表遍历
        for(DicType dt : dtList){

            //取得每一种类型的字典类型编码
            String code = dt.getCode();

            //根据每一个字典类型来取得字典值列表
            List<DicValue> dvList = dicValueDao.getListByCode(code);

            map.put(code+"List", dvList);


        }

        return map;
    }

    public List<DicType> getAll2() {
        List<DicType> dtList = dicTypeDao.getTypeList2();
        return dtList;
    }

    public List<DicValue> getValueByCode(String typeCode) {
        List<DicValue> dvList = dicValueDao.getListByCode(typeCode);
        return dvList;
    }

    public List<DicValue> getAllValue() {
        List<DicValue> dvList = dicValueDao.getAllValue();
        return dvList;
    }

    public boolean save(DicType dt, DicValue dv, String typeDic) {

        boolean flag = true;

        int count = 0;

        if ("dicType".equals(typeDic)){

            count = dicTypeDao.save(dt);

        }else if("dicValue".equals(typeDic)){

            count = dicValueDao.save(dv);

        }

        System.out.println("count = " + count);
        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public boolean deleteByIds(String[] codes, String[] ids) {

        boolean flag =true;
        int count = 0;

        if (codes!=null&&codes.length!=0){

            count = dicTypeDao.deleteByIds(codes);

        }else if(ids!=null&&ids.length!=0){

            count = dicValueDao.deleteByIds(ids);

        }
        System.out.println("count = " + count);
        if (codes!=null&&codes.length!=0){
            if (count != codes.length) {

                flag = false;

            }
        }
        if (ids!=null&&ids.length!=0){
            if (count != ids.length) {

                flag = false;

            }
        }

        return flag;
    }

    public Map<String, Object> getDicType(String code, String id, String typeDic) {

        Map<String, Object> map = new HashMap<String, Object>();

        if ("dicType".equals(typeDic)){
            DicType dt = dicTypeDao.getByCode(code);
            map.put("dt", dt);

        }else if("dicValue".equals(typeDic)){
            DicValue dv = dicValueDao.getById(id);
            map.put("dv", dv);
        }

        //返回map就可以了
        return map;
    }

    public boolean update(DicType dt, DicValue dv, String typeDic) {

        boolean flag =true;
        int count = 0;

        if ("dicType".equals(typeDic)){

            count = dicTypeDao.update(dt);

        }else if("dicValue".equals(typeDic)){

            count = dicValueDao.update(dv);

        }
        if (count != 1) {

            flag = false;

        }
        return flag;
    }


}




















